package com.chinamsp.dydb.dbsource.policy;

import java.util.List;
import java.util.Random;

import org.springframework.util.CollectionUtils;

import com.chinamsp.dydb.dbsource.DataSourceImpl;

/**
 * 随机策略数据源实现
 * @author shifenghu
 *
 */
public class RandomPolicyImpl implements DynamicDataSourcePolicy {

	@Override
	public DataSourceImpl choose(DataSourceImpl master, List<DataSourceImpl> slaves, String moduleName) {
		//如果没有从库，则直接返回主库
		if (CollectionUtils.isEmpty(slaves)) {
			return master;
		}
		// 随机走起
		return slaves.get(new Random().nextInt(slaves.size()));
	}

}
